// pages/cultivation/add_trans/add_trans.js
const http = require("../../../utils/request");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    shake:true,
    change_data:{},
    Code:null,
    amount:0,
    count: 0,
    type: {
      vehicleModel: "车型",
      licenseNumber: "车牌号码",
      driver: "司机",
      destination: "货品去向",
      count: "分拨数量",
      remark: "备注"
    }
  },
  change(v) {
    let index = v.currentTarget.dataset.data
    let value = v.detail.value
    let data = this.data.change_data
    data[index] = value
    console.log(value,data);
    this.setData({
      data,
    })
  },
  handleChange(e){
    this.setData({
      [`change_data.count`]:e.detail.value
    })
  },
  cancel(){
    wx:wx.navigateBack({
      delta: 1,
      success: (res) => {
      },
      fail: (res) => {},
      complete: (res) => {},
    })
  },
  confirm(){
    if(this.data.shake){
      this.setData({
        shake:false,
      })
      http.postRequestBody('/admin/dispatch',JSON.stringify(this.data.change_data),res=>{
        wx:wx.showToast({
          title: '新增成功',
        })
        setTimeout(()=>{
          this.setData({
            shake:true,
          })
          this.cancel();
        },900);
      },err=>{
        wx:wx.showToast({
          title: '失败',
          icon:"error",
        })
        this.setData({
          shake:true,
        })
      })
    }else{
      wx:wx.showToast({
        title: '不要重复点击',
        icon:"error",
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let app = getApp().globalData;
    this.setData({
      Code: app.codeing,
      [`change_data.cultivationCode`]:app.codeing
    })
    http.getRequest("/admin/cultivation/"+app.codeing+"/residue",{},res=> {
      this.setData({
        amount: res.data.data
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})